﻿using System;
using System.Collections.Generic;
using System.Text;

namespace deFramework.BusinessRules
{
    /// <summary>
    /// .
    /// </summary>
    public class SpAttributesTools : Base.BaseBusinessRule
    {

        #region "Constructor, private Variables"

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aContext"></param>
        public SpAttributesTools(ref Common.PresentationContext aContext) : base(ref aContext) { }  
 
 
		#endregion



        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        public bool UserPageDesignPermissions(string pageUrl)
        {
            bool result = false;
            try
            {
                Microsoft.SharePoint.SPFile page = this.GetPageInContextWeb(pageUrl);
                if (page != null)
                    result = this.UserHasPageDesignPermissions(page);
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public bool UserHasPageDesignPermissions(Microsoft.SharePoint.SPFile page)
        {
            bool hasPermission = false;
            try
            {
                Microsoft.SharePoint.SPBasePermissions userPerms;
                try
                {
                    userPerms = page.Item.EffectiveBasePermissions;
                }
                catch
                {
                    userPerms = this.MySpWeb.EffectiveBasePermissions;
                }
                hasPermission = (userPerms &
                    Microsoft.SharePoint.SPBasePermissions.AddDelPrivateWebParts)
                    != 0;
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return hasPermission;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        public Microsoft.SharePoint.SPFile GetPageInContextWeb(string pageUrl)
        {
            Microsoft.SharePoint.SPFile page = null;
            try
            {
                page = this.MySpWeb.GetFile(pageUrl);
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
                //throw ex; 
            }
            return page;
        }


        [Obsolete]
        private Microsoft.SharePoint.SPFile GetAnyPage(string pageUrl)
        {
            Microsoft.SharePoint.SPFile page = null;
            Microsoft.SharePoint.SPWeb objWeb = null;
            try
            {
                Microsoft.SharePoint.SPSite objSite =
                    new Microsoft.SharePoint.SPSite(pageUrl);
                string siteCollectionUrl = objSite.Url;
                string webRelativeUrl = pageUrl.Substring(siteCollectionUrl.Length + 1);
                if (webRelativeUrl.IndexOf("?") > 1)
                    webRelativeUrl = webRelativeUrl.Substring(0, webRelativeUrl.IndexOf("?"));
                objWeb = objSite.OpenWeb(webRelativeUrl, false);
                page = objWeb.GetFile(pageUrl);
                //


            }
            catch { }
            return page;
        }


    }
}
